package com.example.source_share.service.impl.user.account;

import com.example.source_share.pojo.User;
import com.example.source_share.service.impl.utils.UserDetailsImpl;
import com.example.source_share.service.user.account.InfoService;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import java.security.SecureClassLoader;
import java.util.HashMap;
import java.util.Map;

@Service
public class InfoServiceImpl implements InfoService { //返回账户信息

    @Override
    public Map<String, String> getinfo() { //转化为用户信息
        UsernamePasswordAuthenticationToken authenticationToken=
                (UsernamePasswordAuthenticationToken) SecurityContextHolder.getContext().getAuthentication();
        //通过token找到用户信息
        UserDetailsImpl loginUser=(UserDetailsImpl) authenticationToken.getPrincipal();
        User user=loginUser.getUser();
        //System.out.println("找到用户:"+user);
        //存储用户信息，返回
        Map<String,String> map= new HashMap<>();
        map.put("massage","success");
        map.put("id",user.getUserid().toString());
        map.put("name",user.getUsername());
        map.put("majorid",user.getMajorid());
        map.put("grade",user.getGrade());
        map.put("email",user.getEmail());
        map.put("phone_number",user.getPhonenumber());
        map.put("contribution", String.valueOf(user.getContributions()));
        map.put("download", String.valueOf(user.getDownload()));
        map.put("purview",user.getPurview());
        return map; 
    }
}
